using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Xml;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;

/// <summary>
/// 
/// </summary>
public partial class Admin_TestDefault : System.Web.UI.Page
{
    private string _DeCryptedPwd;
    private string _UserName;
    private string _UserDomain;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (User.IsInRole("Users"))
        {
            Response.Redirect("~/NoAccess.htm");
        }

        SFG.Common.Utilities.Encryptor _Encryptor = new SFG.Common.Utilities.Encryptor(SFG.Common.Utilities.Encryptor.SymmProvEnum.Rijndael);
        _DeCryptedPwd = _Encryptor.Decrypting(WebConfigurationManager.AppSettings["UserPwd"], WebConfigurationManager.AppSettings["EncryptKey"]);
        _UserName = WebConfigurationManager.AppSettings["UserAcct"];
        _UserDomain = WebConfigurationManager.AppSettings["UserDomain"];

        if (!IsPostBack)
        {
            string _Path = "";
            _Path = WebConfigurationManager.AppSettings["FileDrop"];

            using (new SFG.Common.Utilities.Impersonator(_UserName, _UserDomain, _DeCryptedPwd))
            {
                String[] _FileList = Directory.GetFiles(_Path, "*.xml");

                this.lstFiles.DataSource = _FileList;
                this.lstFiles.DataBind();
            }
        }
    }


    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnBeginTest_Click(object sender, EventArgs e)
    {
        string _Path = lstFiles.SelectedItem.Text;
        BarcodeProcessor _AppUtil = new BarcodeProcessor();

        if (!String.IsNullOrEmpty(_Path))
        {
            using (new SFG.Common.Utilities.Impersonator(_UserName, _UserDomain, _DeCryptedPwd))
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(_Path);

                string _Path2 = "";
                _Path2 = WebConfigurationManager.AppSettings["FileDrop"];

                XmlElement encriptedElement = (XmlElement)xmlDoc.GetElementsByTagName("EncryptedData")[0];

                AppUtil.DecriptXmlDoc(ref _AppUtil, ref xmlDoc, ref encriptedElement);

                if (xmlDoc != null)
                {
                    string result = _AppUtil.BarCodeGenerator(xmlDoc.OuterXml, _Path2);
                    this.FreeTextBox1.Text = result;
                }
                else
                {
                    this.FreeTextBox1.Text = "No Results";
                }
            }
        }
    }
}
